<?php
namespace app\controllers;

use app\models\AmountRecorder;
use app\models\CouponRecorder;
use app\models\DiscountRecorder;
use app\models\MemberRecorder;
use app\models\NewRecorder;
use app\models\OrderRecorder;
use app\models\PurchaseRecorder;
use app\models\YxnumRecorder;

class HomeController extends BaseController{

    public function actionInfo(){
        $order=OrderRecorder::find();
        $arr['order_price'] = $order->sum('total_price');
        $arr['order_no_pay_price'] = $order->where('is_pay=0')->sum('total_price');
        $arr['order_pay_num'] = $order->where('is_pay=1')->count();
        $arr['order_no_pay_num'] = $order->where('is_pay=0')->count();
        $arr['order_send'] = $order->where('is_pay=1')->andWhere('is_send=0')->count();
        //今天
        $h = date("H",time());
        $time = strtotime("- {$h} hours",time());
        $arr['order_now_price'] = $order->where(['>','addtime',$time])->sum('total_price');
        $arr['order_now_no_pay_price'] = $order->where(['>','addtime',$time])->andWhere('is_pay=0')->sum('total_price');
        $result = ['result'=>1,'data'=>$arr];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }


    public function actionOrdernum(){
        ini_set("memory_limit","2048M");
        set_time_limit(0);
        $post = \Yii::$app->request->post('order_sl_time');
        $b=microtime(true);
        $list = OrderRecorder::find()->select(['id','addtime']);
        if(is_array($post)){
            $start = strtotime($post[0]);
            $end = strtotime($post[1]);
            $d = ceil(($end-$start)/(60*60*24));
            $d_arr = [];
            $return_arr=[];
            for($i=0;$i<=$d;$i++){
                $time = strtotime("+ {$i} days",$start);
                $arr['time'] = date('Y-m-d',$time);
                $arr['num']=0;
                $d_arr[] = $arr['time'];
                $return_arr[]=$arr;
            }
            $list = $list->where(['between','addtime',$start,$end])->all();


            foreach($list as $v){
               $time = date('Y-m-d',$v->addtime);
               $index = array_search($time,$d_arr);
               if($index>=0){
                   $return_arr[$index]['num']+=1;
               }
            }



        }else{
            $post = intval($post);
            $h = date("H",time());
            if(empty($post)){//今天
                $start = strtotime("- {$h} hours",time());
                $end = time();

            }else{
                $end = strtotime("- {$h} hours",time());
                $start = strtotime("- 24 hours",$end);
                $h=24;
            }
            $h_arr=[];
            $return_arr=[];
            for($i=0;$i<=$h;$i++){
                $arr['time'] = $i;
                $arr['num'] = 0;
                $return_arr[]=$arr;
                $h_arr[]=$i;
            }
            $list = $list->where(['between','addtime',$start,$end])->all();

            foreach($list as $v){
                $time = date("H",$v->addtime);
                $index = array_search($time,$h_arr);
                if($index>=0){
                    $return_arr[$index]['num']+=1;
                }
            }
        }




        $result = ['result'=>1,'data'=>$return_arr,'list'=>$list,"time"=>strtotime("-1 days",time())];
        return json_encode($result);
    }


    public function actionMembernum(){
        $type = \Yii::$app->request->post('type');
        $num = MemberRecorder::find()->count();
        switch(intval($type)){
            case 0:
                $cz_num = MemberRecorder::find()->where('cz_num!=0')->count();
                $else_num = $num-$cz_num;
                $arr['value'] = $cz_num;
                $arr['name'] = "充值会员";
                $return_arr[]=$arr;
                $arr['value'] = $else_num;
                $arr['name'] = "未充值会员";
                $return_arr[]=$arr;
                break;
            case 1:
                $cz_num = MemberRecorder::find()->where('total_num>0')->count();
                $else_num = $num-$cz_num;
                $arr['value'] = $cz_num;
                $arr['name'] = "消费会员";
                $return_arr[]=$arr;
                $arr['value'] = $else_num;
                $arr['name'] = "未消费会员";
                $return_arr[]=$arr;
                break;
            case 2:
                $cz_num = MemberRecorder::find()->where('vip>0')->count();
                $else_num = $num-$cz_num;
                $arr['value'] = $cz_num;
                $arr['name'] = "等级会员";
                $return_arr[]=$arr;
                $arr['value'] = $else_num;
                $arr['name'] = "普通会员";
                $return_arr[]=$arr;
                break;
            default:
                $cz_num = MemberRecorder::find()->where('cz_num!=0')->count();
                $else_num = $num-$cz_num;
                $arr['value'] = $cz_num;
                $arr['name'] = "充值会员";
                $return_arr[]=$arr;
                $arr['value'] = $else_num;
                $arr['name'] = "未充值会员";
                $return_arr[]=$arr;
                break;
        }


        $result = ['result'=>1,'data'=>$return_arr];
        return json_encode($result);
    }


    public function actionYxtype(){
        $type = \Yii::$app->request->post('type');
        $arr=[];
        switch(intval($type)){
            case 1://优惠券
                $list = CouponRecorder::find()->asArray()->all();
                foreach($list as $k=>$v){
                    $arr[$k]['cate_name'] = "满".$v['count']."元减".$v['reduce'].'元';
                    $arr[$k]['id'] = $v['id'];
                }
                break;
            case 2://满件减
                $list = DiscountRecorder::find()->asArray()->all();
                foreach($list as $k=>$v){
                    $arr[$k]['id'] = $v['id'];
                    $arr[$k]['cate_name']  = $v['title'];
                }
                break;
            case 3://满额减
                $list = AmountRecorder::find()->asArray()->all();
                foreach ($list as $k=>$v){
                    $arr[$k]['id'] = $v['id'];
                    $arr[$k]['cate_name'] = $v['title'];
                }
                break;
            case 4://限时购
                $list = PurchaseRecorder::find()->asArray()->all();
                foreach($list as $k=>$v){
                    $arr[$k]['id'] = $v['id'];
                    $arr[$k]['cate_name'] = $v['title'];
                }
                break;
            case 5://新人购
                $list = NewRecorder::find()->asArray()->all();
                foreach($list as $k=>$v){
                    $arr[$k]['id'] = $v['id'];
                    $arr[$k]['cate_name'] = $v['title'];
                }
                break;
        }
        $result = ['result'=>1,'data'=>$arr];
        return json_encode($result);
    }


    public function actionYx(){
        $post = \Yii::$app->request->post("form");
        $list = YxnumRecorder::find()->select(['id','addtime','is_pay']);
        $type = intval($post['yx_type']);
        if($type){
            $list = $list->andWhere('yx_type=:type',[':type' => $type]);
        }
        $cate = intval($post['yx_cate']);
        if($cate){
            $list = $list->andWhere('yx_id=:id',[':id' => $cate]);
        }
        $list = $list->orderBy('addtime DESC');

        $time = $post['yx_sl_time'];
        $is_hour=0;
        $arr = [];
        $h_arr=[];
        if(is_array($time)){
            $start = strtotime($time[0]);
            $end = strtotime($time[1]);
            $d = ceil(($end-$start)/(60*60*24));
            $list = $list->andWhere(['between',"addtime",$start,$end]);
            for($i=0;$i<=$d;$i++){
                $item=[];
                $t = strtotime("+ {$i} days",$start);
                $h_arr[]=date('Y-m-d',$t);
                $item[]=date('Y-m-d',$t);
                $item[]=0;
                $item[]=0;
                $arr[]=$item;
            }
        }else{
            $time = intval($time);
            if(empty($time)){
                $h=date('H',time());
                $arr=[];
                $h_arr=[];
                for($i=0;$i<=$h;$i++){
                    $item=[];
                    $h_arr[]=$i;
                    $item[]=$i;
                    $item[]=0;
                    $item[]=0;
                    $arr[]=$item;
                }
                $start_time = strtotime("- {$h} hours",time());
                $list = $list->andWhere(['>=','addtime',$start_time]);
            }else{
                $h=24;
                $arr=[];
                $h_arr=[];
                for($i=0;$i<$h;$i++){
                    $item=[];
                    $h_arr[]=$i;
                    $item[]=$i;
                    $item[]=0;
                    $item[]=0;
                    $arr[]=$item;
                }
                $h_step = 24+date('H',time());
                $start_time = strtotime("- {$h_step} hours",time());
                $end_time = strtotime("+ 1 days",$start_time);
                $list = $list->andWhere(['between',"addtime",$start_time,$end_time]);
            }
            $is_hour=1;
        }
        $list = $list->asArray()->all();//未支付

        foreach($list as $v){
            $is_time = $is_hour?date("H",$v['addtime']):date('Y-m-d',$v['addtime']);
            $index = array_search($is_time,$h_arr);
            if($index>=0){
                if($v['is_pay']){
                    $arr[$index][1] += 1;
                }else{
                    $arr[$index][2] += 1;
                }

            }
        }


        $result = ['result'=>1,'data'=>$arr];
        return json_encode($result);
    }

}
